package controller.admin;

import model.Page;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "AdminUserListServlet", value = "/admin/user_list")
public class AdminUserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService us = new UserService();
        //初始化默认值
        int pageNumber = 1;
        Page page = null;

        //获取参数
        String tempPageNumber = request.getParameter("pageNumber");
        //如果传了就赋值
        if (tempPageNumber != null && !"".equals(tempPageNumber)) {
            //防止页码小于1
            pageNumber = Integer.parseInt(tempPageNumber) <= 0 ? 1 : Integer.parseInt(tempPageNumber);
        }

        try {
            page = us.selectAllUser(pageNumber);
            request.setAttribute("p", page);
            request.getRequestDispatcher("user_list.jsp").forward(request, response);
        } catch (SQLException e) {
            request.getRequestDispatcher("user_list.jsp").forward(request, response);
            e.printStackTrace();
        }

    }
}
